package com.suguowen.sblog.service;

import com.suguowen.sblog.bean.Article;
import com.suguowen.sblog.bean.Tag;
import com.suguowen.sblog.bean.TagExample;
import com.suguowen.sblog.mapper.ArticleMapper;
import com.suguowen.sblog.mapper.DIYMapper;
import com.suguowen.sblog.mapper.TagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TagService {

    @Autowired
    TagMapper tagMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    private DIYMapper diyMapper;

    // 查询所有标签（包含被标签过的文章数量）
    public List<Tag> findContainTagedArtsCountTag(){
        return tagMapper.select();
    }

    // 查询所有标签（不包含被标签过的文章数量）
    public List<Tag> findAllTag(){
        List<Tag> list = tagMapper.selectByExample(new TagExample());
        return list;
    }

    public Tag findTagById(int articleId){
        Tag tag = diyMapper.selectTagByArtId(articleId);
        return tag;
    }

    // 修改标签
    public void modifyTag(Tag tag) {
        tagMapper.updateByPrimaryKey(tag);
    }

    // 删除标签
    @Transactional
    public void deleteTag(Integer tagId) {
        // 把原tag归类成未分类
        articleMapper.updateTagNoneByArtId(tagId);
        // 删除tag
        tagMapper.deleteByPrimaryKey(tagId);
    }
    // 创建标签
    public void createTag(Tag tag) {
        tagMapper.insert(tag);
    }

}
